import React from "react";
import {Modal} from "antd";
import ReactDOM from "react-dom";

import BaseComponent from "../BaseComponent";

export default class Window extends BaseComponent {

    static show(content, options = {}) {
        let htmlNode = document.createElement('div');
        document.body.appendChild(htmlNode);
        let win;
        ReactDOM.render(
            React.createElement(
                Window,
                {
                    ref: (o) => {
                        win = o
                    },
                    htmlNode,
                    ...options
                },
                content
            ), htmlNode);
        return win;
    }

    constructor(props) {
        super(props);
        this.state = {visible: true};
    }

    componentWillUnmount() {
        document.body.removeChild(this.props.htmlNode);
    }

    _onCancel() {
        this.setState({visible: false});
    }

    _onAfterClose() {
        ReactDOM.unmountComponentAtNode(this.props.htmlNode);
    };

    close() {
        this._onCancel();
    }

    render() {

        return (
            <Modal
                title={this.props.title}
                destroyOnClose={true}
                visible={this.state.visible}
                keyboard={false}
                maskClosable={false}
                centered={true}
                footer={null}
                onCancel={this._onCancel.bind(this)}
                afterClose={this._onAfterClose.bind(this)}
                {...this.props}
            >
                {
                    this.props.children
                }
            </Modal>
        )
    }
}